-- 使用DC缓存必须添加触发器 可以使用Oracle触发器

-- 触发器包含三种触发
-- 1.插入后触发
-- 2.删除后触发
-- 3.更新后触发  (先删除后新增)

CREATE OR REPLACE TRIGGER TRG_TB_USER
AFTER INSERT OR DELETE OR UPDATE ON TB_USER
FOR EACH ROW

  BEGIN
    IF INSERTING THEN
      INSERT INTO TB_NOTIFY_LOG (SN, CMD_TYPE, TABLE_NAME, PK_VALUES, WHERE_CONDITION )
      VALUES
        (SEQ_NOTIFY_LOG.NEXTVAL,'INS','TB_USER',:NEW.USER_ID,'USER_ID=''' || :NEW.USER_ID || '''');
    END IF;

    IF DELETING THEN
      INSERT INTO TB_NOTIFY_LOG (SN, CMD_TYPE, TABLE_NAME, PK_VALUES, WHERE_CONDITION)
      VALUES
        (SEQ_NOTIFY_LOG.NEXTVAL,'DEL','TB_USER',:OLD.USER_ID,'USER_ID=''' || :OLD.USER_ID || '''');
    END IF;

    IF UPDATING THEN
      INSERT INTO TB_NOTIFY_LOG (SN, CMD_TYPE, TABLE_NAME, PK_VALUES, WHERE_CONDITION)
      VALUES
        (SEQ_NOTIFY_LOG.NEXTVAL,'DEL','TB_USER',:OLD.USER_ID,'USER_ID=''' || :OLD.USER_ID || '''');

      INSERT INTO TB_NOTIFY_LOG (SN, CMD_TYPE, TABLE_NAME, PK_VALUES, WHERE_CONDITION)
      VALUES
        (SEQ_NOTIFY_LOG.NEXTVAL,'INS','TB_USER',:NEW.USER_ID,'USER_ID=''' || :NEW.USER_ID || '''');
    END IF;
  END TRG_TB_USER;